import {
  GetAll,
  GetList,
  Get,
  Update,
  Delete,
  Create,
  ChangePassword,
  GetTreeList,
  GetAllPermissions,
  GetListByCustAsync,
  EnablePayChanelAsync,
  GetOptsAsync,
  GetAllByPart,
  DelPatch,
  Modify,
  NewObj,
  GetItems,
  ExportExcel,
  InvAdjust,
  AddStock,
  GetIssueMtl,
  IssueMtl,
  ReturnMtl,
} from '@/services/crud';
import { getCurrent } from '@/services/user';
import { Reducer, Effect } from 'umi';

export interface AbpModelType {
  namespace: string;
  state: any;
  effects: {
    get: Effect;
    getAll: Effect;
    getList: Effect;
    update: Effect;
    create: Effect;
    delete: Effect;
    delPatch: Effect;
    changePassword: Effect;
    getTreeList: Effect;
    getAllPermissions: Effect;
    setCurrent: Effect;
    getCurrentUser: Effect;
    getListByCust: Effect;
    enablePayChanel: Effect;
    getOptsAsync: Effect;
    getAllByPart: Effect;
    modify: Effect;
    newObj: Effect;
    getItems: Effect;
    exportExcel: Effect;
    invAdjust:Effect;
    addStock:Effect;
    getIssueMtl:Effect; 
    issueMtl:Effect; 
    returnMtl:Effect; 
  };
  reducers: {
    deal: Reducer<any>;
    setCurrentUser: Reducer<any>;
  };
}

const AbpModel: AbpModelType = {
  namespace: 'Abp',
  state: {
    response: {},
    currentUser: {},
  },
  effects: {
    *get({ payload, control }, { call, put }) {
      const response = yield call(Get, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *getAll({ payload, control }, { call, put }) {
      const response = yield call(GetAll, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *getList({ payload, control }, { call, put }) {
      const response = yield call(GetList, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *update({ payload, control }, { call, put }) {
      const response = yield call(Update, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *create({ payload, control }, { call, put }) {
      const response = yield call(Create, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *delete({ payload, control }, { call, put }) {
      const response = yield call(Delete, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *delPatch({ payload, control }, { call, put }) {
      const response = yield call(DelPatch, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *changePassword({ payload, control }, { call, put }) {
      const response = yield call(ChangePassword, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *getTreeList({ payload, control }, { call, put }) {
      const response = yield call(GetTreeList, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *getAllPermissions({}, { call, put }) {
      const response = yield call(GetAllPermissions);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *getListByCust({ payload }, { call, put }) {
      const response = yield call(GetListByCustAsync, payload);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *enablePayChanel({ payload }, { call, put }) {
      const response = yield call(EnablePayChanelAsync, payload);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *getOptsAsync({ payload }, { call, put }) {
      const response = yield call(GetOptsAsync, payload);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *getAllByPart({ payload, control }, { call, put }) {
      const response = yield call(GetAllByPart, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *getCurrentUser({}, { call, put }) {
      const response = yield call(getCurrent);
      yield put({
        type: 'setCurrentUser',
        payload: response.result?.user,
      });
    },
    *setCurrent({ payload }, { call, put }) {
      yield put({
        type: 'setCurrentUser',
        payload,
      });
    },
    *newObj({ payload, control }, { call, put }) {
      const response = yield call(NewObj, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *modify({ payload, control }, { call, put }) {
      const response = yield call(Modify, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *getItems({ payload, control }, { call, put }) {
      const response = yield call(GetItems, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *exportExcel({ payload, control }, { call, put }) {
      const response = yield call(ExportExcel, payload, control);
      yield put({
        type: 'deal',
        payload: response,
      });
    },
    *invAdjust({payload},{call,put}){
      const response = yield call(InvAdjust,payload);
      yield put({
        type:'deal',
        payload:response,
      })
    },
    *addStock({payload},{call,put}){
      const response = yield call(AddStock,payload);
      yield put({
        type:'deal',
        payload:response,
      })
    },
    *getIssueMtl({payload},{call,put}){
      const response = yield call(GetIssueMtl,payload);
      yield put({
        type:'deal',
        payload:response,
      })
    },
    *issueMtl({payload},{call,put}){
      const response = yield call(IssueMtl,payload);
      yield put({
        type:'deal',
        payload:response,
      })
    },
    *returnMtl({payload},{call,put}){
      const response = yield call(ReturnMtl,payload);
      yield put({
        type:'deal',
        payload:response,
      })
    },
  },
  reducers: {
    deal(state, action) {
      return { ...state, response: action.payload };
    },
    setCurrentUser(state, action) {
      return { ...state, currentUser: action.payload };
    },
  },
};
export default AbpModel;
